<?php 
$req_path="./";
include_once($req_path."include/ini.php");
require_once($req_path."include/db.php");

class M_usergroup extends DB{
	private $dbConn;
	private $rowCount="0";
	private $ID="0";
	private $sqlSelect ="select * from tb_usergroup where 1=1";
	private $totalpage="0";
	
	function M_usergroup(){
		parent::__construct();
	}
	
	function getListPage(){
		global $list_page;
		return $list_page;
	}
	
	function UserPermission($id){
		$sql=" select s.ID, g.GroupID, s.ScreenName, g.`View` , g.`Add` , g.`Edit` , g.`Deleted` "
			. " ,s.`View` as h_view , s.`Add` as h_add , s.`Edit` as h_edit , s.`Deleted` as h_deleted "
			. " from tb_group_permission g "
			. " join tb_screen_list s on s.ID = g.ScreenID "
			. " where g.GroupID=".$id;
		
		//Execute
		$result = $this->ExecuteData($sql);
		//Get all rows
		$rows =$result->RecordCount();
		//Set rows
		$this->rowCount=$rows;
		//Return 
		return $result;		
	}
	
	function getPermission($type="2"){
		$sql= 'select id , ScreenName , `View` , `Add` , `Edit` , `Deleted` '
					. ' from tb_screen_list where 1 = 1 ' ;
					
		if ($type=="2"){
			$sql .= " and ScreenType='".$type."'";
		}
		
		//Execute
		$result = $this->ExecuteData($sql);
		//Get all rows
		$rows =$result->RecordCount();
		//Set rows
		$this->rowCount=$rows;
		//Return 
		return $result;
	}
	function getData($name=null, $type="0", $pageStart=1){
		global $list_page;
		$sqlscript= " select GroupID, GroupName , Discount  , u.Type "
				." , CASE WHEN u.Type =1 THEN 'Head Office' ELSE 'Branch' END GroupType "
				." From tb_usergroup u "
				." Where 1=1 and Status=1";
		
		if (isset($type) && $type!="0"){
			$sqlscript.= " and Type=". $type;
		}
		if (isset($name) && $name!=""){
			$sqlscript.= " and GroupName Like '%". $name."%'";
		}
  
		//Order 
		$sqlscript.= " order by u.GroupName asc " ;
		//Execute
		$result = $this->ExecuteData($sqlscript);
		//Get all rows
		$rows =$result->RecordCount();
		
		//Page limit
		$sqlscript.= " limit ". ($pageStart-1)*$list_page. ", " . $list_page;
		
		//paging
		$result = $this->ExecuteData($sqlscript);
		
		$totalpage=1;
		if ($rows>0){
			$rt = $rows%$list_page;
			
			if($rt!=0) {
				$totalpage = floor($rows/$list_page)+1;
			}
			else {
				$totalpage = floor($rows/$list_page);
			}
		}
		
		$this->rowCount=$rows;
		$this->totalpage=$totalpage;	
		
		return $result;
	}
	
	function getDataDetail($id=0){
		$sql="select  GroupID, GroupName, Discount , u.Type , CASE WHEN u.Type =1 THEN 'Head Office' ELSE 'Branch' END GroupType  "
			." From tb_usergroup u where GroupID=".$id;
			
		$result = $this->ExecuteData($sql);
		return $result->FetchRow();
	}
	
	function getRowCount(){
		return $this->rowCount;
	}
	
	function getLastInsert(){
		return $this->ID;
	}
	function Excute($sql){
		$this->ExecuteData($sql);
	}
	function InsertData($val){
		if (isset($val)){
			$sqlInsert=$this->InsertSqlScript($this->sqlSelect, $val);
			$this->ExecuteData($sqlInsert);
			$this->ID = $this->getLastID();
			return $this->ID;
		}
	}
	function updateData($sql, $val){
		if(isset($val)){
			$sqlUpdate=$this->UpdateSqlScript($sql,$val);
			return $this->ExecuteData($sqlUpdate);
		}
	}
	function delePermission($id){
		$sqlDelete = "delete from tb_group_permission where GroupID ='".$id."'";
		$this->ExecuteData($sqlDelete);
	}
	function delData($id){
		if(isset($id)){
			$sqlDelete = "update  tb_usergroup set  Status=0 where GroupID='".$id."'";
			$this->ExecuteData($sqlDelete);
			return "c";
		}
	}

	function getTotalPage(){
		return $this->totalpage;
	}
}
?>